package com.lanou3g.farmdoctor.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.lanou3g.farmdoctor.R;
import com.lanou3g.farmdoctor.activity.ForRichDetailActivity;
import com.lanou3g.farmdoctor.adapter.ItemRichListAdapter;
import com.lanou3g.farmdoctor.base.BaseFragment;
import com.lanou3g.farmdoctor.entity.ForRichEntity;
import com.lanou3g.farmdoctor.entity.greendao.RichDetailEntity;
import com.lanou3g.farmdoctor.stream.ReadStream;
import com.lanou3g.farmdoctor.volley.VolleySingleton;
import com.lanou3g.farmdoctor.widget.SwipeRefreshLoadingLayout;

/**
 * 本类由: Risky 创建于: 15/11/24.
 * 致富经页面的所有Fragment
 */
public class RichItemFragment extends BaseFragment implements AdapterView.OnItemClickListener, SwipeRefreshLoadingLayout.OnRefreshListener, SwipeRefreshLoadingLayout.OnLoadListener {

    private static final String ARGS_POSITION = "position";
    // 存储需要解析的数据
    private static final int[] FILES = {R.raw.item_news,
            R.raw.item_tech, R.raw.item_expert, R.raw.item_farmer, R.raw.item_mrzhao};
    // 跳转传值的key
    public static final String ARG_RICH_ITEM = "item_rich_data";

    private int position;
    // ListView的Adapter
    private ItemRichListAdapter adapter;
    private Context context;
    // 实体类,存储数据用
    private ForRichEntity entity;

    public static RichItemFragment newInstance(int position) {
        // 通过静态方法初始化本类的对象,传入需要的值
        RichItemFragment f = new RichItemFragment();
        Bundle args = new Bundle();
        args.putInt(ARGS_POSITION, position);
        f.setArguments(args);
        return f;
    }

    // 刷新和加载的控件
    private SwipeRefreshLoadingLayout srLayout;
    private ListView listView;
    private View headerView;

    // 获取网路图片的控件
    private NetworkImageView imgHead;
    private TextView tvTitle;

    // 模拟刷新和加载的loading用
    private Handler handler = new Handler();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_item_rich, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    private void initView(View view) {
        srLayout = (SwipeRefreshLoadingLayout) view.findViewById(R.id.srLayout);
        srLayout.setColor(R.color.refreshLoading_first,
                R.color.refreshLoading_second,
                R.color.refreshLoading_third,
                R.color.refreshLoading_fouth);
        // 绑定刷新和加载的监听
        srLayout.setOnRefreshListener(this);
        srLayout.setOnLoadListener(this);
        listView = (ListView) view.findViewById(R.id.listView);
        // 初始化ListView的头视图
        initHeader(listView);
        listView.addHeaderView(headerView);
    }

    // 初始化ListView的头视图
    private void initHeader(ViewGroup parent) {
        headerView = LayoutInflater.from(getActivity()).inflate(R.layout.header_item_rich, parent, false);
        imgHead = (NetworkImageView) headerView.findViewById(R.id.img_header);
        tvTitle = (TextView) headerView.findViewById(R.id.tv_title);
        imgHead.setDefaultImageResId(R.mipmap.ic_loading_fail);
        imgHead.setErrorImageResId(R.mipmap.ic_loading_fail);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        context = getActivity();
        adapter = new ItemRichListAdapter(context);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
        Bundle args = getArguments();
        if (args != null) {
            // 获取ViewPager的当前页码
            position = args.getInt(ARGS_POSITION);
        }
        // 根据页码读取相应的文件,解析成实体类
        entity = ReadStream.getEntity(ForRichEntity.class, FILES[position]);

        adapter.add(entity.getList());
        // 设置头视图的内容
        imgHead.setImageUrl(entity.getList().get(0).getPicUrl(), VolleySingleton.getImageLoader());
        tvTitle.setText(entity.getList().get(0).getTitle());
    }


    /**
     * 停止刷新和加载的动画
     */
    private void stopAnim() {
        srLayout.setRefreshing(false);
        srLayout.setLoading(false);
    }

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            stopAnim();
        }
    };

    private void cancelRL() {
        handler.postDelayed(runnable, 2000);
    }

    @Override
    public void onLoad() {
        cancelRL();

    }

    @Override
    public void onRefresh() {
        cancelRL();
    }

    /**
     * 点击事件,传递相应的值
     * @param parent
     * @param view
     * @param position
     * @param id
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(context, ForRichDetailActivity.class);
        RichDetailEntity listEntity = null;
        if (position != 0) {
            listEntity = (RichDetailEntity) parent.getAdapter().getItem(position);
        } else {
            listEntity = entity.getList().get(position);
        }

        intent.putExtra(ARG_RICH_ITEM, listEntity);
        context.startActivity(intent);

    }
}
